package com.deyuanyun.pic.ctrl.service.impl;

import com.deyuanyun.pic.common.util.MapUtil;
import com.deyuanyun.pic.ctrl.enumeration.EqptTypeEnum;
import com.deyuanyun.pic.ctrl.service.EqptDataOperationalService;
import com.deyuanyun.pic.pipe.dao.BaseDAO;
import com.deyuanyun.pic.settings.domain.util.DomainCodeInterceptor;
import com.deyuanyun.pic.settings.domain.util.DomainInitUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 *
 * @author lijun
 * @date 2017-01-03
 *
 * History: [修改人][时间][修改描述]
 */
@Service
public class EqptDataOperationalServiceImpl implements EqptDataOperationalService{

    private
    @Resource BaseDAO baseDao;

    @Override
    public int insertSelectiveEqptData(EqptTypeEnum type, Map map) {
        //将需要新增map转换为bean
        Object obj = null;
        try {
            obj = MapUtil.forceMapToObject(map,type.getClazz());
        } catch (Exception e) {
            e.printStackTrace();
        }

        //创建id、时间、用户
        DomainInitUtil.initCreateDomain(obj);
        //调用mapper层方法
        int num = baseDao.insertSelective(obj);
        return num;
    }

    @Override
    public Object selectEqptData(EqptTypeEnum type,String eqptId) {
        //调用mapper层方法
        String[] fieldNames = new String[2];
        Object[] fieldValues = new Object[2];
        //id
        fieldNames[0]="id"; fieldValues[0]=eqptId;
        //deleted
        fieldNames[1]="deleted"; fieldValues[1]=(short)0;

        List list = baseDao.selectByExample(type.getClazz(),fieldNames,fieldValues);
        return list != null && list.size()>0 ? list.get(0):null;
    }

    @Override
    public int updateSelectiveEqptData(EqptTypeEnum type, Map map) {
        //将需要更新map转换为bean
        Object obj = null;
        try {
            obj = MapUtil.forceMapToObject(map,type.getClazz());
        } catch (Exception e) {
            e.printStackTrace();
        }

        //更新时间、用户、备注
        DomainInitUtil.initUpdateDomain(obj);
        //调用mapper层方法
        int num = baseDao.updateByPrimaryKeySelective(obj);
        return num;
    }
}
